# !/usr/bin/python
# -*- coding: UTF-8 -*-
#from ftplib import FTP
#from optparse import OptionParser
#import manifest
#import zipUpdateManifest
#import os
#import shutil
#import datetime
#import jsonFileUtils
#import xmlFileUtils
#import sys
#reload(sys)
#sys.setdefaultencoding('utf8')
#localEncoding = sys.getfilesystemencoding()
#
## H5 构建输出目录
#WEB_MOBILE_PATH = "build/web-mobile"
#H5_PACKAGE_PATH = "../../jenkinsOutputs/H5"
## H5 天天内网测试输出目录
#H5_INNER_NET_TEST_OUT_PATH = "D:/CustomerH5"
#HOT_INNER_NET_TEST_OUT_PATH = "D:/CustomerHotUpdate"
#
## hotUpdate 热更输出目录
#BUILD_RES_PATH = "build/jsb-link/res"
#BUILD_SRC_PATH = "build/jsb-link/src"
#BUILD_DEFAULT_PATH = "build/jsb-link"
#HOT_PACKAGE_PATH = "../../jenkinsOutputs/HotUpdate"
#hotOutPath = ""
#
## apk 输出目录
#BUILD_APK_FILE_NAME = "build/jsb-link/publish/android/liangzi-release-signed.apk"
#BUILD_APK_FILE_NAME_PATH = "build/jsb-link/publish/android"
#BUILD_DEBUG_APK_FILE_NAME = "build/jsb-link/frameworks/runtime-src/proj.android-studio/app/build/outputs/apk/debug/liangzi-debug.apk"
#APK_PACKAGE_PATH = "../../jenkinsOutputs/Android"
#
## gitHubUrl 文件目录
#GITHUBURL_FILE_PATH = "build/jsb-link/res/raw-assets/gitHubUrl.txt"
#H5_GITHUBURL_FILE_PATH = "build/web-mobile/res/raw-assets/gitHubUrl.txt"
#
## ios 外网正式，外网测试打包资源res,src由windows构建而来
#IOS_PACK_RES = "build-templates/iosPackRes/res"
#IOS_PACK_SRC = "build-templates/iosPackRes/src"
## ios 内网测试，打包资源res,src由windows构建而来
#IOS_INNER_PACK_RES = "build-templates/iosInnerPackRes/res"
#IOS_INNER_PACK_SRC = "build-templates/iosInnerPackRes/src"
#
#
## 公共方法
#def getCustomPath():
#    customName = os.environ["CustomerBranch"]
#    print('CustomerBranch =', customName)
#    # customName = 'main/PreRelease'
#    names = customName.split('/')
#    if len(names) < 2:
#        return names[0]
#    customPath = os.path.join(names[0], names[1])
#    for i in range(2, len(names)):
#        customPath = os.path.join(customPath, names[i])
#    return customPath
#
#
#def getCurFolderName():
#    nowTime = datetime.datetime.now()
#    folderName = nowTime.strftime('%Y-%m-%d %H.%M.%S')
#    print(folderName)
#    customName = getCustomName()
#    folderName = folderName + '_' + customName
#    print('folderName =', folderName)
#    return folderName
#
#
## 用jsonFileUtils里面获取配置的接口
#def getConfigCustomName():
#    jsonFileUtils.loadConfigData()
#    jsonFileUtils.getCurConfig()
#    return jsonFileUtils.getCustomerName()
#
#
#def getCustomName():
#    # customName = os.environ["CustomerBranch"]
#    # names = customName.split('/')
#    # if len(names)<3 :
#    #    return names[0]
#    # return names[len(names)-1]
#    # 读取打包配置表
#    customerName = getConfigCustomName()
#    print('customerName x =', customerName)
#    netType = os.environ['NetType']
#    buildType = os.environ['BuildType']
#    apkFileName = customerName + "_" + buildType + "_" + netType
#    return apkFileName
#
#
#def getLocalEncodeString(strMsg):
#    return strMsg.decode('utf-8').encode(localEncoding)
#
#
## ---------------------H5---------------------------
#def getH5OutputPath():
#    tempPath = os.path.join(os.getcwd(), H5_PACKAGE_PATH)
#    print('get custom name =', getCustomPath())
#    customPath = os.path.join(tempPath, getCustomPath())
#    timeFolderPath = os.path.join(customPath, getCurFolderName())
#    realPath = os.path.join(timeFolderPath, "web-mobile")
#    print(realPath)
#    h5Path = os.path.abspath(realPath)
#    return h5Path
#
#
#def copyH5ToOutputPath():
#    h5OutPath = getH5OutputPath()
#    print(h5OutPath)
#    h5PackPath = os.path.join(os.getcwd(), WEB_MOBILE_PATH)
#    print(h5PackPath)
#    h5PackRealPath = os.path.abspath(h5PackPath)
#    print(h5PackRealPath)
#    h5PackRealPath = getLocalEncodeString(h5PackRealPath)
#    h5OutPath = getLocalEncodeString(h5OutPath)
#    shutil.copytree(h5PackRealPath, h5OutPath)
#    print('copy h5 end!')
#    # dest：zip输出目录 source：被压缩目录
#    # 压缩上一级目录
#    h5ZipOutPath = os.path.join(h5OutPath, '../')
#    shutil.make_archive(h5ZipOutPath, "zip", h5ZipOutPath)
#    # 如果是内网测试，拷贝一份到222机器上
#    if os.environ['NetType'] == 'inner_test':
#        print('copy to apache')
#        outPath = os.environ['OutPath']
#        print('outPath:' + outPath)
#        h5OutPath = ''
#        if outPath == 'default':
#            print('use customerName')
#            customerName = getConfigCustomName()
#            customerNamePath = os.path.join(H5_INNER_NET_TEST_OUT_PATH,
#                                            customerName)
#            h5OutPath = os.path.abspath(customerNamePath)
#        else:
#            print('use OutPath')
#            _outPath = os.path.join(H5_INNER_NET_TEST_OUT_PATH, outPath)
#            h5OutPath = os.path.abspath(_outPath)
#
#        h5OutPath = getLocalEncodeString(h5OutPath)
#        print('abs tiantian inner test =', h5OutPath)
#        if os.path.exists(h5OutPath):
#            shutil.rmtree(h5OutPath)
#        shutil.copytree(h5PackRealPath, h5OutPath)
#
#
## ---------------------热更新--------------------------
#def getHotOutputPath():
#    tempPath = os.path.join(os.getcwd(), HOT_PACKAGE_PATH)
#    print('get custom name =', getCustomPath())
#    customPath = os.path.join(tempPath, getCustomPath())
#    timeFolderPath = os.path.join(customPath, getCurFolderName())
#    realPath = timeFolderPath
#    print(realPath)
#    hotOutPath = os.path.abspath(realPath)
#    return hotOutPath
#
#
#def copyHotToOutputPath():
#    hotOutPath = getHotOutputPath()
#    print(hotOutPath)
#
#    netType = os.environ['NetType']
#    print('netType =', netType)
#    zipUpdateUrl = jsonFileUtils.getZipUpdateUrlByNetType(netType)
#    if zipUpdateUrl != 0:
#        hotOutPath = getLocalEncodeString(hotOutPath)
#        zipUpdateManifest.makeZipUpdate(BUILD_DEFAULT_PATH, hotOutPath,
#                                        zipUpdateUrl)
#    else:
#        hotResOutPath = os.path.join(hotOutPath, "res")
#        hotSrcOutPath = os.path.join(hotOutPath, "src")
#        print(hotResOutPath)
#        print(hotSrcOutPath)
#        buildResPath = os.path.abspath(
#            os.path.join(os.getcwd(), BUILD_RES_PATH))
#        buildSrcPath = os.path.abspath(
#            os.path.join(os.getcwd(), BUILD_SRC_PATH))
#        buildResPath = getLocalEncodeString(buildResPath)
#        buildSrcPath = getLocalEncodeString(buildSrcPath)
#        print('buildResPath =', buildResPath)
#        print('buildSrcPath =', buildSrcPath)
#        hotResOutPath = getLocalEncodeString(hotResOutPath)
#        hotSrcOutPath = getLocalEncodeString(hotSrcOutPath)
#        print('hotResOutPath =', hotResOutPath)
#        print('hotSrcOutPath =', hotSrcOutPath)
#        shutil.copytree(buildResPath, hotResOutPath)
#        shutil.copytree(buildSrcPath, hotSrcOutPath)
#        print('copy res src end!')
#        print('start generate hotupdate manifest')
#        manifest.makeHallHotUpdateManifest(hotOutPath)
#        manifest.makeGamesHotUpdateManifest(hotOutPath)
#
#    # dest：zip输出目录 source：被压缩目录
#    shutil.make_archive(hotOutPath, "zip", hotOutPath)
#
#    print('copy zip to 222 CustomerZip')
#    ftp = FTP()
#    print ftp.connect('10.10.100.222', '21')
#    print ftp.login('', '')
#    fp = open(hotOutPath + ".zip", 'rb')
#    cmd = 'STOR HotUpdate/CustomerZip/' + getConfigCustomName() + '.zip'
#    print ftp.storbinary(cmd, fp)
#    print 'copy to 222 finish'
#
#    # 如果是内网测试，拷贝一份到222机器上
#    netType = os.environ['NetType']
#    if netType == 'inner_test':
#        hotInnerOutPath = os.path.abspath(HOT_INNER_NET_TEST_OUT_PATH)
#        hotInnerOutPath = getLocalEncodeString(hotInnerOutPath)
#        if os.path.exists(hotInnerOutPath):
#            shutil.rmtree(hotInnerOutPath)
#        shutil.copytree(hotOutPath, hotInnerOutPath)
#
#
## ---------------------生成apk-------------------------
#def getApkOutputPath():
#    print('get custom name =', getCustomPath())
#    customPath = os.path.join(os.getcwd(), APK_PACKAGE_PATH)
#    customPath = os.path.join(customPath, getCustomPath())
#    timeFolderPath = os.path.join(customPath, getCurFolderName())
#    apkOutPath = os.path.abspath(timeFolderPath)
#    return apkOutPath
#
#
#def copyApkToOutputPath():
#    netType = os.environ['NetType']
#    if netType == 'inner_test':
#        tempApkName = BUILD_DEBUG_APK_FILE_NAME
#        tempPath = os.path.join(os.getcwd(), tempApkName)
#        apkPath = os.path.abspath(tempPath)
#    else:
#        tempPath = os.path.abspath(
#            os.path.join(os.getcwd(), BUILD_APK_FILE_NAME_PATH))
#        apkName = os.listdir(tempPath)[0]
#        apkPath = os.path.join(tempPath, apkName)
#
#    print("src apk=", apkPath)
#    apkOutPath = getApkOutputPath()
#    print("dest apk=", apkOutPath)
#    os.makedirs(apkOutPath)
#    customerName = getCustomName()
#    print('customerName apk=', customerName)
#    apkFileName = customerName + ".apk"
#    print('apkFileName =', apkFileName)
#    apkOutFilePath = os.path.join(apkOutPath, apkFileName)
#    apkPath = getLocalEncodeString(apkPath)
#    apkOutFilePath = getLocalEncodeString(apkOutFilePath)
#    shutil.copy(apkPath, apkOutFilePath)
#    print('copy apk end!')
#
#
#
#def makeHallHotConfig():
#    buildDefaultPath = os.path.abspath(
#        os.path.join(os.getcwd(), BUILD_DEFAULT_PATH))
#    print('buildDefaultPath =', buildDefaultPath)
#
#    netType = os.environ['NetType']
#    print('netType =', netType)
#    zipUpdateUrl = jsonFileUtils.getZipUpdateUrlByNetType(netType)
#    if zipUpdateUrl != 0:
#        zipUpdateManifest.makHallApkConfig(buildDefaultPath, zipUpdateUrl)
#    else:
#        manifest.makHallApkConfig(buildDefaultPath)
#
#
## 根据NetType的值修改网络环境
#def changeNetType():
#    global GITHUBURL_FILE_PATH
#    netType = os.environ['NetType']
#    #INVITEID = os.environ['INVITEID']
#    print('netType =', netType)
#    #print('INVITEID =', INVITEID)
#    # 加载打包配置
#    jsonFileUtils.loadConfigData()
#    jsonFileUtils.getCurConfig()
#    gitHubUrl = jsonFileUtils.getGitHubUrlByNetType(netType)
#    bigVersion = jsonFileUtils.getBigVersion()
#    # gitHubUrl = gitHubUrl.replace("'",'\"')
#    print('gitHubUrl =', gitHubUrl)
#    customerName = os.environ['CustomerBranch']
#    if customerName == 'Customer/template1/wanmei':
#        global GITHUBURL_FILE_PATH
#        GITHUBURL_FILE_PATH = "build/jsb-link/res/raw-assets/resources/gitHubUrl.txt"
#    gitHubUrlPath = os.path.join(os.getcwd(), GITHUBURL_FILE_PATH)
#    print(gitHubUrlPath)
#    gitHubUrlPath = os.path.abspath(gitHubUrlPath)
#    print('abs =', gitHubUrlPath)
#    gitHubUrlJsonData = jsonFileUtils.getJsonDataFromFile(gitHubUrlPath)
#    gitHubUrlJsonData['gitHubUrl'] = gitHubUrl
#    gitHubUrlJsonData['VERSION_APK'] = int(bigVersion)
#    gitHubUrlJsonData['netType'] = netType
#    #gitHubUrlJsonData['INVITEID'] = INVITEID
#    #print('gitHubUrlJsonData[''] =', gitHubUrlJsonData['INVITEID'])
#    jsonFileUtils.saveJsonDataToFile(gitHubUrlPath, gitHubUrlJsonData)
#    # f = open(gitHubUrlPath,'w')
#    # f.write(gitHubUrl)
#    # f.close()
#
#
#def changeH5NetType():
#    netType = os.environ['NetType']
#    print('netType h5 =', netType)
#    jsonFileUtils.loadConfigData()
#    jsonFileUtils.getCurConfig()
#    gitHubUrl = jsonFileUtils.getGitHubUrlByNetType(netType)
#    bigVersion = jsonFileUtils.getBigVersion()
#    print('gitHubUrl =', gitHubUrl)
#    gitHubUrlLen = len(gitHubUrl)
#    print('gitHubUrlLen =', gitHubUrlLen)
#    if gitHubUrlLen < 4:
#        print('less than 4')
#        return
#    # gitHubUrl = gitHubUrl.replace("'",'\"')
#    print('gitHubUrl h5 =', gitHubUrl)
#    customerName = os.environ['CustomerBranch']
#    if customerName == 'Customer/template1/wanmei':
#        global H5_GITHUBURL_FILE_PATH
#        H5_GITHUBURL_FILE_PATH = "build/web-mobile/res/raw-assets/resources/gitHubUrl.txt"
#    print('githuburlpath =', H5_GITHUBURL_FILE_PATH)
#    gitHubUrlPath = os.path.join(os.getcwd(), H5_GITHUBURL_FILE_PATH)
#    print(gitHubUrlPath)
#    gitHubUrlPath = os.path.abspath(gitHubUrlPath)
#    print('abs =', gitHubUrlPath)
#    gitHubUrlJsonData = jsonFileUtils.getJsonDataFromFile(gitHubUrlPath)
#    gitHubUrlJsonData['gitHubUrl'] = gitHubUrl
#    gitHubUrlJsonData['VERSION_APK'] = int(bigVersion)
#    gitHubUrlJsonData['netType'] = netType
#    jsonFileUtils.saveJsonDataToFile(gitHubUrlPath, gitHubUrlJsonData)
#    # f = open(gitHubUrlPath,'w')
#    # f.write(gitHubUrl)
#    # f.close()
#
#
## 修改app名
#def changeAndroidAppName():
#    netType = os.environ['NetType']
#    customerName = os.environ['CustomerBranch']
#    if netType == 'inner_test':
#        xmlFileUtils.setAndroidAppName('内网测试')
#    elif netType == 'online_test':
#        if customerName != 'Customer/template2/liangzi':
#            xmlFileUtils.setAndroidAppName('外网测试')
#
#
#def changeIosAppName():
#    netType = os.environ['NetType']
#    customerName = os.environ['CustomerBranch']
#    if netType == 'inner_test':
#        xmlFileUtils.setIosAppName('内网测试')
#    elif netType == 'online_test':
#        if customerName != 'Customer/template2/liangzi':
#            xmlFileUtils.setIosAppName('外网测试')
#
#
#def copyIosPackRes():
#    netType = os.environ['NetType']
#    destResPath = os.path.join(os.getcwd(), BUILD_RES_PATH)
#    destResPath = os.path.abspath(destResPath)
#    destSrcPath = os.path.join(os.getcwd(), BUILD_SRC_PATH)
#    destSrcPath = os.path.abspath(destSrcPath)
#    shutil.rmtree(destResPath)
#    shutil.rmtree(destSrcPath)
#    sourceResPathTemp = IOS_PACK_RES
#    sourceSrcPathTemp = IOS_PACK_SRC
#    if netType == 'inner_test':
#        sourceResPathTemp = IOS_INNER_PACK_RES
#        sourceSrcPathTemp = IOS_INNER_PACK_SRC
#    sourceResPath = os.path.join(os.getcwd(), sourceResPathTemp)
#    sourceResPath = os.path.abspath(sourceResPath)
#    sourceSrcPath = os.path.join(os.getcwd(), sourceSrcPathTemp)
#    sourceSrcPath = os.path.abspath(sourceSrcPath)
#    shutil.copytree(sourceResPath, destResPath)
#    shutil.copytree(sourceSrcPath, destSrcPath)
#    print('sourceResPath =', sourceResPath)
#    print('destResPath =', destResPath)
#    print('sourceSrcPath =', sourceSrcPath)
#    print('destSrcPath =', destSrcPath)
#
#
#if __name__ == "__main__":
#    parser = OptionParser()
#    parser.add_option('-t', '--buildType', dest="buildType")
#    (option, args) = parser.parse_args()
#